import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.Scanner;

public class Client {

    private String host;
    private int port;

    private String name;

    private String to;
    private Bootstrap bootstrap;

    private EventLoopGroup group;

    private ChannelFuture channelFuture;

    public Client(String host, int port, String name, String to) {
        this.host = host;
        this.port = port;
        this.name = name;
        this.to = to;
        this.bootstrap = new Bootstrap();
        this.group = new NioEventLoopGroup();
    }

    public void run() {
        try {
            doConnect();
            // 获取客户端的通道给服务端发消息
            Channel channel = channelFuture.channel();
            IMessage login = new IMessage();
            login.setType(MessageTypeEnum.LOGIN);
            login.setFrom(name);
            login.setTo("服务器");
            login.setBody("请求登录");
            channel.writeAndFlush(login);
            while (true) {
                Scanner scanner = new Scanner(System.in);
                String inp = scanner.nextLine();
                // 创建ByteBuf
                IMessage iMessage = new IMessage();
                iMessage.setType(MessageTypeEnum.REQUEST);
                iMessage.setBody(inp);
                iMessage.setFrom(name);
                iMessage.setTo(to);
                // 将内容写到byteBuf中
//                byteBuf.writeBytes((inp + System.getProperty("line.separator")).getBytes("UTF-8"));
                // 将buff写到管道中
                channel.writeAndFlush(iMessage);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }

    public void doConnect() {
        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Client client = this;
            bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) {
                    socketChannel.pipeline().addLast(new IdleStateHandler(4, 0, 0));
//                    socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
                    socketChannel.pipeline().addLast(new MessageEncoder());
                    socketChannel.pipeline().addLast(new MessageDecoder());
                    socketChannel.pipeline().addLast(new TimeClientHandler());
                    // 消息需要放在编码、解码器之后
                    socketChannel.pipeline().addLast(new HeartbeatClientHandler());
                    socketChannel.pipeline().addLast(new ClientReConnectHandler(client));
                }
            }).remoteAddress(host, port);
            channelFuture = bootstrap.connect().addListener(new ConnectionListener(client)).sync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
